{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# url = 'http://bj.ganji.com/ershoufang/'\n",
    "url = 'https://bj.58.com/ershoufang/'\n",
    "\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.get(url, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "html = etree.HTML(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120\n"
     ]
    }
   ],
   "source": [
    "li_list = html.xpath('.//li[@class=\"sendsoj\"]')\n",
    "print(len(li_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['中粮别墅区大平层  精装修 一梯一户  南北通透\\xa0'] ['1199'] ['万'] ['53527元/㎡']\n",
      "['养老宜居 胜芳古镇 4A级景区 垂钓园 送小院 \\xa0'] ['29'] ['万'] ['2990元/㎡']\n",
      "['京南高铁站现房特价3000！外地可买可贷二十分进\\xa0'] ['33.5'] ['万'] ['3642元/㎡']\n",
      "['金百一 北大街小区  无税 简装 六层 配套齐全\\xa0'] ['240'] ['万'] ['28916元/㎡']\n",
      "['北京新国贸  近地铁 大产权 外地人可买 随时网\\xa0'] ['95'] ['万'] ['11875元/㎡']\n",
      "['买房送车位，北京东国贸商圈，配套齐全外地人可买首\\xa0'] ['62'] ['万'] ['13192元/㎡']\n",
      "['京南现房特价2800 电梯洋房 70年产权独立房\\xa0'] ['31'] ['万'] ['3648元/㎡']\n",
      "['地铁旁 精装修 无遮挡 小两居 总价低 有钥匙看\\xa0'] ['167'] ['万'] ['25892元/㎡']\n",
      "['瀛海家园低楼层有房本满二 诚心卖随时将签约价格便\\xa0'] ['275'] ['万'] ['32315元/㎡']\n",
      "['宜居1万1  融城园南区婚房出售 3室2厅 30\\xa0'] ['305'] ['万'] ['26294元/㎡']\n",
      "['行宫一里，低楼层三居室，满五年出售，前后无遮挡\\xa0'] ['315'] ['万'] ['24892元/㎡']\n",
      "['西南五环 卢沟桥 杜家坎旁中层精装两居 产权清晰\\xa0'] ['215'] ['万'] ['34943元/㎡']\n",
      "['大兴机场旁荣盛开发，特价7500，首付7万，门口\\xa0'] ['62'] ['万'] ['7470元/㎡']\n",
      "['西单 金融街 2005年小区 通透大两居 封闭式\\xa0'] ['1210'] ['万'] ['110644元/㎡']\n",
      "['清苍甩卖，1周必卖，南北通透户型方正215万出售\\xa0'] ['215'] ['万'] ['17339元/㎡']\n",
      "['霸州孔雀城特价房 现房，捡 漏房源，来电有优惠 \\xa0'] ['55'] ['万'] ['6471元/㎡']\n",
      "['纳帕溪谷，花园300平，1250万，精装修超*\\xa0'] ['1250'] ['万'] ['38344元/㎡']\n",
      "['珠江壹千栋 经典小户型 业主诚意出售 看房有钥匙\\xa0'] ['1450'] ['万'] ['49153元/㎡']\n",
      "['梧桐苑 南北通透 独立两居 豪华装修 100平米\\xa0'] ['220'] ['万'] ['22000元/㎡']\n",
      "['高铁现房 特价3800 电咨 送小花园 就几套 \\xa0'] ['26'] ['万'] ['3612元/㎡']\n",
      "['特价3999 70年大产权 交通便利 外地人可购\\xa0'] ['41'] ['万'] ['4824元/㎡']\n",
      "['26万一套一居室 3o1医院旁特惠4288元涿州\\xa0'] ['33'] ['万'] ['4025元/㎡']\n",
      "['通州南 高铁旁 外地可买可贷！京津冀经济重要枢纽\\xa0'] ['34'] ['万'] ['3696元/㎡']\n",
      "['雄 安新区特价3300 小区内自带观景湖垂钓园 \\xa0'] ['36'] ['万'] ['4338元/㎡']\n",
      "['晨浩花园联排别墅 总价低 看房有钥匙\\xa0'] ['510'] ['万'] ['35665元/㎡']\n",
      "['悠享海居生活，让你离海更近，将假日旅行变成生活中\\xa0'] ['69'] ['万'] ['7841元/㎡']\n",
      "['大兴机场南，高速出口旁，首付六万，购一层送一层，\\xa0'] ['31'] ['万'] ['6459元/㎡']\n",
      "['（新上）新机场 地铁口高端住宅 均价7700  \\xa0'] ['80'] ['万'] ['7693元/㎡']\n",
      "['新双路~09年房~首付44万~业主着急出售~有地\\xa0'] ['165'] ['万'] ['18791元/㎡']\n",
      "['檀城东区 独立两居 南北通透 精装修 185万 \\xa0'] ['185'] ['万'] ['21495元/㎡']\n",
      "['房山 城关 看房方便 一居小户型 诚心出售 看房\\xa0'] ['88'] ['万'] ['22565元/㎡']\n",
      "['(刚需现房)北京南 特价4299 70年大产权 \\xa0'] ['40'] ['万'] ['3739元/㎡']\n",
      "['北京东燕郊唯一4.5米精装房（地铁800米）买（\\xa0'] ['108'] ['万'] ['12950元/㎡']\n",
      "['永安里~正规2居带客厅~81平米~275万~房主\\xa0'] ['275'] ['万'] ['33951元/㎡']\n",
      "['京南G铁D铁房！交通便利！年底交房！大开发商荣盛\\xa0'] ['58'] ['万'] ['7250元/㎡']\n",
      "['珠江御景425万138平米3室2厅 电梯房保养好\\xa0'] ['420'] ['万'] ['30435元/㎡']\n",
      "['御园 4室3厅6卫 客厅朝南 格局方正\\xa0'] ['4099'] ['万'] ['111417元/㎡']\n",
      "['海淀 四季青 碧桐园 紧邻公交地铁 格局全改 可\\xa0'] ['175'] ['万'] ['53031元/㎡']\n",
      "['房山南 首付16万 2室2厅 交通便利 上市开发\\xa0'] ['62'] ['万'] ['8158元/㎡']\n",
      "['新上 客厅带窗两居室 青塔秀园 带电梯\\xa0'] ['328'] ['万'] ['48723元/㎡']\n",
      "['雄 安新区东，现房一居室，一层送花园，高铁旁，外\\xa0'] ['19.5'] ['万'] ['3306元/㎡']\n",
      "['裕华园 精装修 一室一厅 满五唯一 高层无遮挡 \\xa0'] ['142'] ['万'] ['24974元/㎡']\n",
      "['润景公馆门头沟出售润景公馆两居室满五\\xa0'] ['338'] ['万'] ['38336元/㎡']\n",
      "['（现房急售）牛驼温泉孔雀城联排中间户 有房本\\xa0'] ['215'] ['万'] ['11383元/㎡']\n",
      "['北京西 涿州现房特价4000 70年大产权紧邻高\\xa0'] ['23'] ['万'] ['4036元/㎡']\n",
      "['开发商回款特惠 首付300万 奥北联排别墅 私家\\xa0'] ['758'] ['万'] ['29154元/㎡']\n",
      "['新机场南 临地铁高铁 通勤北京20芬钟 大产权环\\xa0'] ['68'] ['万'] ['8096元/㎡']\n",
      "['水景官邸（康居南一区）~视野好~紧挨着公园 停车\\xa0'] ['275'] ['万'] ['22553元/㎡']\n",
      "['通州南大兴机场地铁现房（3588）一层带小院顶层\\xa0'] ['38'] ['万'] ['3519元/㎡']\n",
      "['大兴机场南，高速出口旁，首付六万，购一层送一层，\\xa0'] ['31'] ['万'] ['6459元/㎡']\n",
      "['北京周边特价房2999，高铁旁成熟配套紧邻四条高\\xa0'] ['30.6'] ['万'] ['3600元/㎡']\n",
      "['宜居1万1 庙城916车站附近怡安园小区三居室\\xa0'] ['200'] ['万'] ['26667元/㎡']\n",
      "['光织谷东区 满五唯一 精装修 两室一厅 高层采光\\xa0'] ['240'] ['万'] ['28146元/㎡']\n",
      "['碧桂园 蔚蓝汇 送精装修 送车位  离永旺100\\xa0'] ['41.5'] ['万'] ['10922元/㎡']\n",
      "['北京东燕郊唯一4.5米精装房（地铁800米）买（\\xa0'] ['108'] ['万'] ['12950元/㎡']\n",
      "['北京大兴南 全款27万起！户型方正低密洋房一梯两\\xa0'] ['27'] ['万'] ['3600元/㎡']\n",
      "['大兴新机场旁，现房养老基地，旅游景区，送菜地，送\\xa0'] ['27.9'] ['万'] ['2847元/㎡']\n",
      "['350万 实际174平米 林肯挑空复式 看房方便\\xa0'] ['520'] ['万'] ['59771元/㎡']\n",
      "['北京南现房即买即住 首付8万得两居 外地可买大产\\xa0'] ['22'] ['万'] ['3385元/㎡']\n",
      "['紧邻地铁特价房 津兴城际直达北京 赠花园赠阁楼温\\xa0'] ['28.4'] ['万'] ['3342元/㎡']\n",
      "['大兴国际机场旁 现房 外地人可网签 北京新亦庄 \\xa0'] ['45'] ['万'] ['8824元/㎡']\n",
      "['花样年华 三居 398万\\xa0'] ['395'] ['万'] ['27847元/㎡']\n",
      "['清逸西园 旧宫中心地 带精装修 近地铁大一居 价\\xa0'] ['200'] ['万'] ['41348元/㎡']\n",
      "['珠江壹千栋 经典小户型 业主诚意出售 看房有钥匙\\xa0'] ['1450'] ['万'] ['49153元/㎡']\n",
      "['77平 满五住房 249万 精装修 2室1厅 看\\xa0'] ['225'] ['万'] ['28865元/㎡']\n",
      "['北京通州东 首付8万 D铁房 外地人可买 独立房\\xa0'] ['103'] ['万'] ['13038元/㎡']\n",
      "['首付40万燕郊买两居 福城上上城五期 精装修 采\\xa0'] ['128'] ['万'] ['14546元/㎡']\n",
      "['雄 安旁紧邻高铁特价首付10万起 送菜地即买即住\\xa0'] ['19.4'] ['万'] ['3345元/㎡']\n",
      "['林肯时代奢装现房 交房软装全齐 总价400万起拎\\xa0'] ['610'] ['万'] ['53044元/㎡']\n",
      "['买64平方，实际交房138平方 精装修复式 大产\\xa0'] ['62'] ['万'] ['9688元/㎡']\n",
      "['北京南现房特价2800 电梯洋房 70年产权独立\\xa0'] ['30'] ['万'] ['3615元/㎡']\n",
      "['大兴南4A景区旁特价房3999 五证齐全直接网签\\xa0'] ['39'] ['万'] ['4699元/㎡']\n",
      "['降价龙湖西区大三居两卫 高楼层视野好 有钥匙随时\\xa0'] ['499'] ['万'] ['36778元/㎡']\n",
      "['北京通州东， 大厂万科新都荟，高品质洋房，全装，\\xa0'] ['265'] ['万'] ['18276元/㎡']\n",
      "['天通苑北一区满五两居，电梯板楼南向采光好钥匙随时\\xa0'] ['440'] ['万'] ['41510元/㎡']\n",
      "['固安南R1地铁旁单价6700起！高品质社区配套齐\\xa0'] ['58'] ['万'] ['7436元/㎡']\n",
      "['北苑万达商圈 首付80万得南北一居可做两居 低楼\\xa0'] ['225'] ['万'] ['42477元/㎡']\n",
      "['北京南高铁旁特价3000，紧邻四大高速，送花园送\\xa0'] ['24'] ['万'] ['3381元/㎡']\n",
      "['雄 安新区70年产权 高铁旁 4A景区房 带小院\\xa0'] ['33'] ['万'] ['3368元/㎡']\n",
      "['首创澜茵山南北户型~明厨明卫采光好~看房随时~拎\\xa0'] ['215'] ['万'] ['17339元/㎡']\n",
      "['融城园南区 送车棚 无税 诚意出售 可议  看房\\xa0'] ['203'] ['万'] ['21665元/㎡']\n",
      "['新居里，南北向两居，首付60万，无税，急售，看房\\xa0'] ['195'] ['万'] ['27269元/㎡']\n",
      "['潘家园小区  中间楼层  无税送车棚  首付低 \\xa0'] ['162'] ['万'] ['36000元/㎡']\n",
      "['单价低两居，南北通透好格局，楼前无遮挡采光好\\xa0'] ['308'] ['万'] ['28087元/㎡']\n",
      "['（96平米现价售300万）林肯公园二期 好朝向 \\xa0'] ['540'] ['万'] ['56250元/㎡']\n",
      "['府前观邸 低楼层 一层带花园 三居双卫 格局好 \\xa0'] ['470'] ['万'] ['37243元/㎡']\n",
      "['通州副中心 岩峰云裳 首付5万 地铁800米 校\\xa0'] ['45'] ['万'] ['10000元/㎡']\n",
      "['70年产权 临近高铁 外地人可购 交通便利 成熟\\xa0'] ['46.5'] ['万'] ['3720元/㎡']\n",
      "['京南第.一站首二机场旁现房 特价6500 70年\\xa0'] ['62'] ['万'] ['7488元/㎡']\n",
      "['亦庄豪宅大平层四居 （首付630万）即可入住 品\\xa0'] ['1500'] ['万'] ['54368元/㎡']\n",
      "['沙河车站首付70万待拆迁范围可**可**大两居免\\xa0'] ['225'] ['万'] ['31035元/㎡']\n",
      "['高铁现房 特价3800 电咨 送小花园 就几套 \\xa0'] ['26'] ['万'] ['3612元/㎡']\n",
      "['光织谷西区~电梯房~265万~满五唯一~客厅通阳\\xa0'] ['265'] ['万'] ['29776元/㎡']\n",
      "['现房！大社区！特价4200！配套齐全！三个月下房\\xa0'] ['55'] ['万'] ['4297元/㎡']\n",
      "['东四环，保利东郡南北三居，高楼层采光，全明格局，\\xa0'] ['1800'] ['万'] ['106768元/㎡']\n",
      "['昌平区回龙观 朱辛庄地铁站 万科TBD新房.现房\\xa0'] ['160'] ['万'] ['40000元/㎡']\n",
      "['雄 安东五证齐全特价3999一平紧邻景区小区内部\\xa0'] ['34'] ['万'] ['4250元/㎡']\n",
      "['涿州70年大产均价4000 准现房 临高铁站 距\\xa0'] ['22.8'] ['万'] ['4000元/㎡']\n",
      "['国企开发 送大花园，给换房周期，送私家电梯 奥北\\xa0'] ['750'] ['万'] ['32328元/㎡']\n",
      "['后沙峪 空中豪宅 地鉄旁 喜缺户型 随时看房\\xa0'] ['280'] ['万'] ['55151元/㎡']\n",
      "['御汤山熙园 5室6厅7卫 401.0平米 189\\xa0'] ['1899'] ['万'] ['47357元/㎡']\n",
      "['千禧家园两居室南北朝向中楼层 环境好\\xa0'] ['188'] ['万'] ['21747元/㎡']\n",
      "['高铁一站直达北京 准现房特价3000送精装修温泉\\xa0'] ['21'] ['万'] ['3044元/㎡']\n",
      "['北京东 首付10万 大产权 开发商直售 直接网签\\xa0'] ['106'] ['万'] ['12046元/㎡']\n",
      "['新出 北影 京承高速15口 大平层三居 通透 采\\xa0'] ['500'] ['万'] ['26179元/㎡']\n",
      "['疫情特价3800 雄 安东大门 养生胜地  温泉\\xa0'] ['31.8'] ['万'] ['3313元/㎡']\n",
      "['佣金1.2，华润小区 2室 77.61\\xa0'] ['400'] ['万'] ['51540元/㎡']\n",
      "['好房必买~湖南小区~(北区)~三楼 三居 ~21\\xa0'] ['210'] ['万'] ['22176元/㎡']\n",
      "['南厂住宅~双卧朝南~低楼层~价格可聊~环境好~车\\xa0'] ['153'] ['万'] ['29965元/㎡']\n",
      "['准现房均价3800送小院送精装送车位首付10万住\\xa0'] ['24'] ['万'] ['3637元/㎡']\n",
      "['京南霸州荣盛大盘70年大产。首付20万五证齐全外\\xa0'] ['72'] ['万'] ['8471元/㎡']\n",
      "['顺鑫澜庭 86平米228万南北通透两居室，一层带\\xa0'] ['228'] ['万'] ['26698元/㎡']\n",
      "['内部员工房(12000)送精装修(送车位)首付4\\xa0'] ['45'] ['万'] ['11843元/㎡']\n",
      "['价格真 实公园地产金茂悦科技住宅，三居室，全明格\\xa0'] ['730'] ['万'] ['54723元/㎡']\n",
      "['新双路三层，三居室，精装修，2009年房，南北通\\xa0'] ['185'] ['万'] ['21069元/㎡']\n",
      "['亦庄金茂悦 双卧朝南三居 精装修采光充足，楼间距\\xa0'] ['875'] ['万'] ['53705元/㎡']\n",
      "['优山美地D区 合院别墅 就这么一套 错过再无\\xa0'] ['2450'] ['万'] ['62026元/㎡']\n",
      "['4200特价！70年大产！有五证！民水民电！门口\\xa0'] ['50'] ['万'] ['4033元/㎡']\n",
      "['通州新政府 近地铁 首付6万 精装校区房 复式二\\xa0'] ['56'] ['万'] ['12445元/㎡']\n",
      "['西南两居大明厅，满五唯一随时看房电梯直达\\xa0'] ['450'] ['万'] ['61899元/㎡']\n"
     ]
    }
   ],
   "source": [
    "for li_item in li_list:\n",
    "    title = li_item.xpath('.//div[@class=\"list-info\"]/h2[@class=\"title\"]/a/text()')\n",
    "    price = li_item.xpath('.//div[@class=\"price\"]/p[@class=\"sum\"]/b/text()')\n",
    "    wan = li_item.xpath('.//div[@class=\"price\"]/p[@class=\"sum\"]/text()')\n",
    "    unit = li_item.xpath('.//div[@class=\"price\"]/p[@class=\"unit\"]/text()')\n",
    "    print(title, price, wan, unit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
